/**
 * 
 */
package any.json.support;

import java.util.Map;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonBeanProcessor;

import org.springframework.beans.factory.config.AbstractFactoryBean;

/**
 * @author keke<keke@codehaus.org>
 * @version $Revision: 31 $
 */
public class JsonConfigFactoryBean extends AbstractFactoryBean {
	private Map<Class<?>, JsonBeanProcessor>	beanProcessors;
	private JsonConfig	                     copyFrom;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.config.AbstractFactoryBean#getObjectType()
	 */
	@Override
	public Class<?> getObjectType() {
		return JsonConfig.class;
	}

	public void setBeanProcessors(
	        final Map<Class<?>, JsonBeanProcessor> beanProcessors) {
		this.beanProcessors = beanProcessors;
	}

	public void setCopyFrom(final JsonConfig copyFrom) {
		this.copyFrom = copyFrom;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
	 */
	@Override
	protected Object createInstance() throws Exception {
		final JsonConfig config = copyFrom == null ? new JsonConfig()
		        : copyFrom.copy();
		if (beanProcessors != null) {
			for (final Class<?> bean : beanProcessors.keySet()) {
				config
				        .registerJsonBeanProcessor(bean, beanProcessors
				                .get(bean));
			}
		}
		return config;
	}
}
